package authenticate;

import Base.ResourceTest;
import com.huawei.translate.model.AccessToken;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
public class When_request_delete_expired_token extends ResourceTest {

    @Test
    public void should_delete_expired_access_token() {

        CreateAccessToken(getTimeStampOnedayAgo());
        CreateAccessToken(System.currentTimeMillis());

        this.testRestTemplate.delete("/api/oauth");
        Assert.assertEquals(1, accessTokenRepository.findAll().size());
    }

    private void CreateAccessToken(long timeStampOnedayAgo) {
        AccessToken accessToken = new AccessToken();
        accessToken.setAppKey("appKey");
        accessToken.setAppSecret("appSecret");
        accessToken.setApplyTime(timeStampOnedayAgo);
        accessTokenRepository.save(accessToken);
    }

    long getTimeStampOnedayAgo() {
        return System.currentTimeMillis() - 1000 * 60 * 60 * 60 * 24;
    }
}
